<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Models\Api\Histroy;
use App\Models\Api\Question;
use App\Models\Api\Reservation;
use App\Models\Api\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ExamController extends BaseController
{

    /**
     * 预约考试
     */
    public function reservation(Request $request, $id)
    {
        try {
            $params = $request->all();
            $validator = Validator::make($params, [
                'endtime' => 'required'
            ]);
            if ($validator->fails()) {
                return $this->responseData([], $validator->errors(), 400);
            }
            $params['createtime'] = now();
            $params['userid'] = $id;
            if (Reservation::create($params)) {
                $user =  User::find($id);
                $m_c = new MessageController;
                $r = new Request(['title' => '考试预约', 'content' => "恭喜" . $user['name'] . "您 预约考试成功,考试将在 " . $params['endtime'] . "举行", 'userid' => $id]);
                $m_c->add($r);
                return $this->responseData([], '预约成功', 200);
            } else {
                return $this->responseData([], '预约失败,请稍后再试', 400);
            }
        } catch (\Throwable $th) {
            $errorInfo = isset($th->errorInfo) ? $th->errorInfo : "这是一个html $th";
            $this->error_log(__FILE__, __LINE__, json_encode($errorInfo));
            return  $this->responseData($errorInfo, '系统错误', 400);
        }
    }

    // /**
    //  * 模拟考试
    //  */
    // public function simulation($id)
    // {
    //     try {
    //         $request = new Request(['type' => -1]);
    //         $q_c = new QuestionController;
    //         $question =  $q_c->index($request)->original['data'];

    //         return $this->responseData($question, '', 200);
    //     } catch (\Throwable $th) {
    //         $errorInfo = isset($th->errorInfo) ? $th->errorInfo : "这是一个html $th";
    //         $this->error_log(__FILE__, __LINE__, json_encode($errorInfo));
    //         return  $this->responseData($errorInfo, '系统错误', 400);
    //     }
    // }

    /**
     *评分
     */
    public function score(Request $request, $id)
    {
        try {
            $params = $request->all();
            $validator = Validator::make($params, [
                'questionList' => "required",
                'flag' => 'required'
            ]);

            $flag = isset($params['flag']) ? $params['flag'] : 1;
            $questionList = json_decode($params['questionList']);
            $questionIdList = [];
            $score = 0;

            if ($validator->fails()) {
                return $this->responseData([], $validator->errors(), 400);
            }
            foreach ($questionList as $item) {

                array_push($questionIdList, intval($item->id));
                if ($item->choice == '') {
                    // 跳过未选择的题目
                    continue;
                }
                // $item['option'] = str_split($item['option']);
                // $item['option'] = implode('', sort($item['option']));
                $question = Question::where('id', '=', $item->id)->first();
                $count1 = count_chars($item->choice, 1);
                $count2 = count_chars($question->answer, 1);

                // 使用 array_diff_assoc 检查两个关联数组的差异
                $diff = array_diff_assoc($count1, $count2);

                // 如果差异为空数组，表示两个关联数组相等
                if (empty($diff)) {
                    $score += $question->score;
                }
            }

            Histroy::create([
                'questionids' => json_encode($questionIdList),
                'userid' => $id,
                'flag' => intval($flag), 'score' => $score, 'time' => now(), 'status' => 1
            ]);
            return $this->responseData(['score' => $score], '评分完成', 200);
            // return $this->responseData($questionIdList, '评分完成', 200);
        } catch (\Throwable $th) {
            $errorInfo = isset($th->errorInfo) ? $th->errorInfo : "这是一个html $th";
            $this->error_log(__FILE__, __LINE__, json_encode($errorInfo));
            return  $this->responseData($errorInfo, '系统错误', 400);
        }
    }
}
